﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;

namespace BingVis.SilverlightClient
{
    // TODO: This whole child window is dirty, clean it up.. make it more MVVM.

    public class ProgressContext : ViewModelBase
    {

        private int totalItems;
        public int TotalItems
        {
            get { return totalItems; }
            set
            {
                if (totalItems != value)
                {
                    totalItems = value;
                    OnPropertyChanged("TotalItems");
                    OnPropertyChanged("ProgressPercent");
                }
            }
        }

        private int itemsProcessed;
        public int ItemsProcessed
        {
            get { return itemsProcessed; }
            set
            {
                if (itemsProcessed != value)
                {
                    itemsProcessed = value;
                    OnPropertyChanged("ItemsProcessed");
                    OnPropertyChanged("ProgressPercent");
                }
            }
        }

        public double ProgressPercent
        {
            get
            {
                if (ItemsProcessed == 0)
                    return 0;
                return (TotalItems / ItemsProcessed) * 100;
            }

        }
    }

    public partial class CompressFileWindow : ChildWindow
    {
        MainShellViewModel _model;
        ProgressContext _progress = new ProgressContext();
        
        public CompressFileWindow(MainShellViewModel model)
        {
            InitializeComponent();

            _model = model;

            this.DataContext = _model;

            this.ProgressHolder.DataContext = _progress;

            AppMediator.Instance.Register<CompressionCompletedEvent>(this, x => OnCompressionCompleted(x.Content));
            AppMediator.Instance.Register<CompressionStartedEvent>(this, x => _progress.TotalItems = x.Content);
            AppMediator.Instance.Register<CompressionProgressEvent>(this, x => _progress.ItemsProcessed = x.Content);

            
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            _model.ShowCompressedFareFileSaver.Execute(null);
            _progress.ItemsProcessed = 0;
            _progress.TotalItems = 500000;
            
            if (_model.SelectedCompressFile != null && _model.SelectedFareFile != null)
            {
                ToggleButtons(false);

                try
                {
                    ProgressHolder.Visibility = Visibility.Visible;
                    _model.CompressFareFile.Execute(new MainShellViewModel.CompressCommandArgs() { CompressFile = _model.SelectedCompressFile, FareFile = _model.SelectedFareFile });
                }
                catch
                {
                    MessageBox.Show("Aw snap, there was a problem compressing the file. Sorry");
                }
            }
            else
                MessageBox.Show("Please make sure to select a fare file and a place to save the compressed file.");

        }

        void ToggleButtons(bool e)
        {
            btnCancel.IsEnabled = e;
            btnOK.IsEnabled = e;
            btnSelect.IsEnabled = e;
        }

        void OnCompressionCompleted(Stream s)
        {
            MessageBox.Show("Compression Completed");
            ToggleButtons(true);
            _progress.ItemsProcessed = 0;
            ProgressHolder.Visibility = Visibility.Collapsed;


            this.DialogResult = true;
            this.Close();
        }
    }
}
